package com.mh.www.mh_frame.util;

import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.mh.www.mh_frame.app.application.MyApplication;

/**
 * toast工具类
 * @author think
 * @version [版本号, 2018/1/16]
 * @see [相关类/方法]
 * @since [V1]
 */

public class ToastUtil {
    private static Toast toastUtil;

    /**
     * 短时间显示Toast【居下】
     * @param msg 显示的内容-字符串*/
    public static void showShortToast(String msg) {
        if(MyApplication.getAppContext() != null){
            if (toastUtil == null) {
                toastUtil = Toast.makeText(MyApplication.getAppContext(), msg, Toast.LENGTH_SHORT);
            } else {
                toastUtil.setText(msg);
            }
            toastUtil.show();
        }
    }
    /**
     * 短时间显示Toast【居中】
     * @param msg 显示的内容-字符串*/
    public static void showShortToastCenter(String msg){
        if(MyApplication.getAppContext() != null) {
            if (toastUtil == null) {
                toastUtil = Toast.makeText(MyApplication.getAppContext(), msg, Toast.LENGTH_SHORT);
                toastUtil.setGravity(Gravity.CENTER, 0, 0);
            } else {
                toastUtil.setText(msg);
            }
            toastUtil.show();
        }
    }

    /**
     * 短时间显示Toast【居上】
     * @param msg 显示的内容-字符串*/
    public static void showShortToastTop(String msg){
        if(MyApplication.getAppContext() != null) {
            if (toastUtil == null) {
                toastUtil = Toast.makeText(MyApplication.getAppContext(), msg, Toast.LENGTH_SHORT);
                toastUtil.setGravity(Gravity.TOP, 0, 0);
            } else {
                toastUtil.setText(msg);
            }
            toastUtil.show();
        }
    }

    /**
     * 短时间显示带图片的Toast [局下]
     * @param msg  显示的内容-字符串
     * @param resId 图片资源
     */
    public static void showShortToastWithPic(String msg,int resId){
        if (MyApplication.getAppContext() != null){
            if (toastUtil == null){
                toastUtil = Toast.makeText(MyApplication.getAppContext(),msg,Toast.LENGTH_SHORT);
            }else {
                toastUtil.setText(msg);
            }
            LinearLayout linearLayout = (LinearLayout) toastUtil.getView();
            ImageView imageView = new ImageView(MyApplication.getAppContext());
            imageView.setImageResource(resId);
            linearLayout.addView(imageView);
            toastUtil.show();
        }
    }

    /**
     * 长时间显示Toast【居下】
     * @param msg 显示的内容-字符串*/
    public static void showLongToast(String msg) {
        if(MyApplication.getAppContext() != null) {
            if (toastUtil == null) {
                toastUtil = Toast.makeText(MyApplication.getAppContext(), msg, Toast.LENGTH_LONG);
            } else {
                toastUtil.setText(msg);
            }
            toastUtil.show();
        }
    }
    /**
     * 长时间显示Toast【居中】
     * @param msg 显示的内容-字符串*/
    public static void showLongToastCenter(String msg){
        if(MyApplication.getAppContext() != null) {
            if (toastUtil == null) {
                toastUtil = Toast.makeText(MyApplication.getAppContext(), msg, Toast.LENGTH_LONG);
                toastUtil.setGravity(Gravity.CENTER, 0, 0);
            } else {
                toastUtil.setText(msg);
            }
            toastUtil.show();
        }
    }
    /**
     * 长时间显示Toast【居上】
     * @param msg 显示的内容-字符串*/
    public static void showLongToastTop(String msg){
        if(MyApplication.getAppContext() != null) {
            if (toastUtil == null) {
                toastUtil = Toast.makeText(MyApplication.getAppContext(), msg, Toast.LENGTH_LONG);
                toastUtil.setGravity(Gravity.TOP, 0, 0);
            } else {
                toastUtil.setText(msg);
            }
            toastUtil.show();
        }
    }

    /**
     * 长时间显示带图片的Toast [局下]
     * @param msg
     * @param resId
     */
    public static void showLongToastWithPic(String msg,int resId){
       if (MyApplication.getAppContext() != null){
           if (toastUtil == null){
               toastUtil = Toast.makeText(MyApplication.getAppContext(),msg,Toast.LENGTH_LONG);
           }else {
               toastUtil.setText(msg);
           }
           LinearLayout linearLayout = (LinearLayout) toastUtil.getView();
           ImageView imageView = new ImageView(MyApplication.getAppContext());
           imageView.setImageResource(resId);
           linearLayout.addView(imageView);
           toastUtil.show();
       }
    }
}
